package org.nfIt.controller;

import com.github.pagehelper.PageInfo;
import org.nfIt.dto.system.AssginRoleDto;
import org.nfIt.dto.system.SysUserDto;
import org.nfIt.entity.system.SysRole;
import org.nfIt.entity.system.SysUser;
import org.nfIt.service.SysUserService;
import org.nfIt.vo.common.Result;
import org.nfIt.vo.common.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ClossName:SysUserController
 * Pakage:org.nfIt.controller
 * Description
 *
 * 用户管理
 *
 *
 * @Author L
 * @Create 2024/6/21 15:18
 * @Version 2023.1.3
 */
@RestController
@RequestMapping(value = "/admin/system/sysUser")
public class SysUserController {


    @Autowired
    private SysUserService sysUserService;

    /**
     * 条件分页查询
     * @param sysUserDto 条件
     * @param pageNum 当前页
     * @param pageSize 每页记录数
     */
    @GetMapping(value = "/findByPage/{pageNum}/{pageSize}")
    public Result<PageInfo<SysRole>> findByPage(SysUserDto sysUserDto ,
                                                @PathVariable(value = "pageNum") Integer pageNum ,
                                                @PathVariable(value = "pageSize") Integer pageSize) {
        PageInfo<SysUser> pageInfo = sysUserService.findByPage(sysUserDto , pageNum , pageSize) ;

        return Result.build(pageInfo , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 添加用户
     * @param sysUser 用户实体
     */
    @PostMapping(value = "/saveSysUser")
    public Result saveSysUser(@RequestBody SysUser sysUser) {
        sysUserService.saveSysUser(sysUser) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 修改用户
     * @param sysUser
     * @return
     */
    @PutMapping(value = "/updateSysUser")
    public Result updateSysUser(@RequestBody SysUser sysUser) {
        sysUserService.updateSysUser(sysUser) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }


    /**
     * 删除用户
     * @param userId
     * @return
     */
    @DeleteMapping(value = "/deleteById/{userId}")
    public Result deleteById(@PathVariable(value = "userId") Long userId) {
        sysUserService.deleteById(userId) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }


    /**
     * 给用户分配角色
     * @param assginRoleDto 封装的实体
     */
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleDto assginRoleDto) {
        sysUserService.doAssign(assginRoleDto) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

}
